// pages/tabBar/search/searchResult/searchReasult.js
import { request } from '../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],  //搜索结果
    value: "",    //搜索内容
    currentPage: "",  //记录当前页
    searchName: "",   //搜索内容
    isNomore: false,   //用于控制是否还有更多的显示
    class: {            //清楚按钮的样式控制
      clear: "clear"
    },
    show: false,        //筛选按钮的开关控制
    filtList: {         //渲染筛选列表  
      type: [
        {
          class: "active",
          text: "全部类型"
        },
        {
          img: "word",
          type:"word",
          text: "文档"
        },
        {
          img: "excel",
          type:"excel",
          text: "表格"
        },
        {
          img: "img",
          type:"image",
          text: "图片"
        },
        {
          img: "ppt",
          type:"ppt",
          text: "幻灯片"
        },
        {
          img: "PDF",
          type:"pdf",
          text: "PDF"
        }
      ],
      source: [
        {
          class: "active",
          text: "全部人员"
        },
        {
          text: "仅自己"
        }
      ],
      time: [
        {
          class: "active",
          text: "全部时间"
        },
        {
          text: "一天内"
        },
        {
          text: "一周内"
        },
        {
          text: "半年内"
        },
        {
          text: "一年内"
        },
      ],
    },
    filtResult: {        //筛选结果的控制
      contributor: null,
      type: null,
      start_time: null,
      end_time: null,
    },
    buttons: [           //筛选页的按钮
      {
        type: 'default',
        className: '',
        text: '重置',
        value: 0
      },
      {
        type: 'primary',
        className: 'sure',
        text: '确定',
        value: 1,
        color: "#487FF8"
      }
    ],
    dateInterval: { '一天内': 1, '一周内': 7, '一个月内': 30, '半年内': 182, '一年内': 365 }  //用于计算筛选的时间

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const name = options.name
    let end_time = this.getBeforeDate(new Date(), 0)
   
    this.setData({
      ...this.data,
      value: name,
      filtResult: {
        ...this.data.filtResult,
        end_time: end_time
      }
    })
    if (name) {
      this.setData({
        ...this.data,
        class: {
          clear: "clear-show"
        }
      })
    }
    this.search();
  },

  /**
   * 页面上拉触底事件的处理函数
   * 手动加页进行下一页数据获取，再拼接
   */
  onReachBottom: function () {
    const name = this.data.searchName
    const page = this.data.currentPage + 1
    const that = this
    const param = {
      search: name,
      start_step: page,
      ...this.data.filtResult
    }
    request("POST", "/api/service/search", param, "", true)
      .then(
        (res) => {
          if (res.data.data_list) {
            for (let i = 0; i < res.data.data_list.length; i++) {
              if (res.data.data_list[i].highlight) {
                if (res.data.data_list[i].highlight.content) {
                  let a = res.data.data_list[i].highlight.content[0].replace(/<em>/ig, '<em style="color:red;font-style:normal" >')
                  res.data.data_list[i].highlight.content[0] = a
                }
                if (res.data.data_list[i].highlight.title) {
                  let a = res.data.data_list[i].highlight.title[0].replace(/<em>/ig, '<em style="color:red;font-style:normal" >')
                  res.data.data_list[i].highlight.title[0] = a
                }
              }

            }
            const newList = this.data.fileList.concat(res.data.data_list)
            that.setData({
              ...that.data,
              currentPage: page,
              searchName: name,
              fileList: newList
            })
          } else {

            that.setData({
              ...that.data,
              isNomore: true,
              fileList: this.data.fileList
            })
          }


        }
      )
  },

  /**
   * 清除
   */
  clear: function () {
    this.setData({
      ...this.data,
      value: "",
      class: {
        clear: "clear"
      }
    })
  },
  /**
   * 搜索
   */
  doSearch: function () {
    this.search();
  },
  /**
   * 搜索的方法
   */
  search: function () {
    const name = this.data.value
    const that = this
    const param = {
      search: name,
      start_step: 1,
      // size:100,
      ...this.data.filtResult
    }
    request("POST", "/api/service/search", param, "", true)
      .then(
        (res) => {//通过正则将样式替换进返回的数据中
          if (res.data.data_list) {
            for (let i = 0; i < res.data.data_list.length; i++) {
              if (res.data.data_list[i].highlight) {
                if (res.data.data_list[i].highlight.content) {
                  let a = res.data.data_list[i].highlight.content[0].replace(/<em>/ig, '<em style="color:red;font-style:normal" >')
                  res.data.data_list[i].highlight.content[0] = a
                }
                if (res.data.data_list[i].highlight.title) {
                  let a = res.data.data_list[i].highlight.title[0].replace(/<em>/ig, '<em style="color:red;font-style:normal" >')
                  res.data.data_list[i].highlight.title[0] = a
                }
              }

            }
            that.setData({
              ...that.data,
              currentPage: 1,
              searchName: name,
              fileList: res.data.data_list
            })
          } else {
            that.setData({
              ...that.data,
              fileList: []
            })
          }


        }
      )
  },
  /**
   * 键盘输入事件
   */
  input: function (e) {
    this.setData({
      ...this.data,
      value: e.detail.value
    })
    if (e.detail.value.length > 0) {
      this.setData({
        ...this.data,
        class: {
          clear: "clear-show"
        }
      })
    } else {
      this.setData({
        class: {
          clear: "clear"
        }
      })
    }
  },
  /**
   * 筛选按钮的触发
   */
  filtrate() {
    this.setData({
      ...this.data,
      show: true
    })
  },
  /**
 * 获取指定天数间距的日期
 * @param {*} myDate 指定那一天
 * @param {*} selectDay 时间间隔
 */
  getBeforeDate(myDate, selectDay) {
    let lw = new Date(myDate - 1000 * 60 * 60 * 24 * selectDay);
    let lastY = lw.getFullYear();
    let lastM = lw.getMonth() + 1;
    let lastD = lw.getDate();
    let startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD);
    return startdate;
  },
  /**
   * 半屏弹窗上的按钮
   */
  buttontap(e) {
    
    if (e.detail.index === 1) {
      this.search()
      this.setData({
        ...this.data,
        show: false
      })
    } else {
      const filtList = this.data.filtList
      filtList.type.map((item) =>{
        if (item.class) {
          delete item.class
        }
      })
      filtList.type[0].class = "active"
      filtList.source.map((item) =>{
        if (item.class) {
          delete item.class
        }
      })
      filtList.source[0].class = "active"
      filtList.time.map((item) =>{
        if (item.class) {
          delete item.class
        }
      })
      filtList.time[0].class = "active"

      this.setData({
        ...this.data,
        filtList:filtList,
        show:false,
        filtResult: {
          ...this.data.filtResult,
          contributor: null,
          type: null,
          start_time: null,
        }
      })
      this.search();
    }

  },
  /**
   * 关闭弹窗时触发
   */
  closeEvent(){
    
    this.setData({
      ...this.data,
      show:false
    })
  },
  /**
   * 弹窗-分类的点击
   * @param {*} e 
   */
  typeTap(e) {
    const tapNum = e.currentTarget.dataset.id
    const { type } = this.data.filtList
    type.map((item, index) => {
      if (item.class) {
        delete item.class
      }

    })
    type[tapNum].class = "active"
    const newTypeList = type
    const filtType = type[tapNum].type ? type[tapNum].type : null
    
    this.setData({
      ...this.data,
      filtList: {
        ...this.data.filtList,
        type: newTypeList,
      },
      filtResult: {
        ...this.data.filtResult,
        type: filtType
      }
    })
  },
  /**
   * 弹窗-上传者的点击
   * @param {*} e 
   */
  uploadManTap(e) {
    const tapNum = e.currentTarget.dataset.id
    const { source } = this.data.filtList
    source.map((item, index) => {
      if (item.class) {
        delete item.class
      }
    })
    source[tapNum].class = "active"
    const newTypeList = source
    const filtType = source[tapNum].text === "全部人员" ? null : wx.getStorageSync('userInfo').first_name
    this.setData({
      ...this.data,
      filtList: {
        ...this.data.filtList,
        source: newTypeList,
      },
      filtResult: {
        ...this.data.filtResult,
        contributor: filtType
      }
    })
  },
  /**
   * 弹窗-时间的点击
   * @param {*} e 
   */
  timeTap(e) {
    const tapNum = e.currentTarget.dataset.id
    const { time } = this.data.filtList
    time.map((item, index) => {
      if (item.class) {
        delete item.class
      }
    })
    time[tapNum].class = "active"
    const newTypeList = time
    const start_time = time[tapNum].text === "全部时间" ? null : this.getBeforeDate(new Date(), this.data.dateInterval[time[tapNum].text])
    
    this.setData({
      ...this.data,
      filtList: {
        ...this.data.filtList,
        time: newTypeList,
      },
      filtResult: {
        ...this.data.filtResult,
        start_time: start_time
      }
    })
  }
})